using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

using System.Configuration;

namespace ORMConsoleApp
{
    using ORM;
    using DAL.sql2005.common;

    class doORM
    {
        private const string C_DB_CONN = "defaultDB";
        private const string C_LOC_OBJ = "BusinessObjectClassesLocation";
        private const string C_LOC_MNG = "ManagerClassesLocation";
        private const string C_KEY_CREATE_SPROCS = "CreateStoredProcedures";
        private const string C_KEY_EXCLUSIONS = "Exclusions";

        private const string C_SQL_GETTABLES = "SELECT * FROM sysObjects WHERE type = 'U' ORDER BY [name]";
        private const string C_GENERATE_CRUD_SPROCS = "pr__SYS_MakeCRUDSprocs";

        private const string C_MSG_PRESS_KEY_TO_START = "Press Any Key to Start \n\n\n\n";
        private const string C_MSG_TITLE = "errORM v0.2b - http://www.errumm.co.uk\r\n";
        private const string C_MSG_ERROR_OCCURRED = "Unfortunately an error occured: \r\n";
        private const string C_MSG_CONTACT = "For more information or support, please get in touch. email : info@errumm.co.uk\r\n";
        private const string C_MSG_COMPLETE = "Classes and Managers created PRESS ANY KEY TO CLOSE THIS WINDOW!\r\n";

        private const string C_ERROR_APPCONFIG = "Your Application.config file has not been setup correctly : email info@errumm.co.uk fo details.\r\n";

        private const string C_MSG_INTRO =
            "errORM - www.errumm.co.uk - 2008\r\n" +
            "\r\n" +
            "You are about to attempt generation of CRUD objects and managers and stored procedures.\r\n" +
            "\r\n" +
            "Database Connnection to use:{0}\r\n" +
            "Business Objects Folder :{1}\r\n" +
            "Managers Folder:{2}\r\n" +
            "Create Stored Procedures?:{3}\r\n" +
            "\r\n\r\n" +
            "TO CANCEL CLOSE THIS WINDOW NOW!" +
            "\r\n\r\n";

        private const string C_YES = "Yes";
        private const string C_NO = "No";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string[] Exclusions;
            string BaseObjectLocation, BaseManagerLocation;
            bool CreateSprocs = false;
            try
            {
                //BusinessObjectClassesLocation
                BaseObjectLocation = System.Configuration.ConfigurationSettings.AppSettings[C_LOC_OBJ];

                if (!Directory.Exists(BaseObjectLocation))
                    Directory.CreateDirectory(BaseObjectLocation);

                //ManagerObjectClassesLocation
                BaseManagerLocation = System.Configuration.ConfigurationSettings.AppSettings[C_LOC_MNG];
                if (!Directory.Exists(BaseManagerLocation))
                    Directory.CreateDirectory(BaseManagerLocation);

                //Create sprocs ? 
                CreateSprocs =
                    Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings[C_KEY_CREATE_SPROCS]);

                Exclusions =
                    System.Configuration.ConfigurationSettings.AppSettings[C_KEY_EXCLUSIONS].Split(Convert.ToChar(","));
            }
            catch
            {
                Console.WriteLine(C_ERROR_APPCONFIG);
                Console.ReadKey();
                return;
            }

            Console.Title = C_MSG_TITLE;
            Console.WriteLine(String.Format(
                C_MSG_INTRO,
                ConfigurationManager.ConnectionStrings[C_DB_CONN],
                BaseObjectLocation,
                BaseManagerLocation,
                CreateSprocs ? C_YES : C_NO));

            Console.Write(C_MSG_PRESS_KEY_TO_START);
            Console.ReadKey();

            DataTable DBTables;
            bool NoErrors = true;
            //try
            //{
            DataAccessManager db = new DataAccessManager(C_DB_CONN);

            DBTables = db.GetDataTableFromSQLText(C_SQL_GETTABLES);
            db.Dispose();

            foreach (DataRow r in DBTables.Rows)
            {
                if ((Array.IndexOf(Exclusions,
                    r["Name"].ToString())
                    ) <0)
                {
                    ObjectCreator oc = new ObjectCreator(r["Name"].ToString(), C_DB_CONN);

                    oc.CreateBusinessObjectClass(BaseObjectLocation + r["Name"].ToString() + ".cs");
                    oc.CreateManagerClass(BaseManagerLocation + r["Name"].ToString() + "Manager.cs");

                    if (oc != null)
                        oc = null;

                    if (CreateSprocs)
                        db.ExecuteNonQuery(C_GENERATE_CRUD_SPROCS, r["Name"].ToString());
                }
            }
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(
            //        String.Format("{0}\r\n{1}\r\n{2}",
            //            C_MSG_ERROR_OCCURRED,
            //            ex.Message,
            //            C_MSG_CONTACT)
            //            );

            //    NoErrors = false;
            //}
            //finally
            //{
            //    DBTables = null;
            //}

            if (NoErrors) Console.WriteLine(C_MSG_COMPLETE);
            else
                Console.WriteLine(C_MSG_ERROR_OCCURRED);


            Console.WriteLine(C_MSG_CONTACT);
            Console.ReadKey();
            Console.Clear();
            return;

        }
    }
}
